home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cpptut22.zip / CHAP01.TXT < prev    next >
Text File  |  1992-01-20  |  23KB  |  531 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                Chapter 1
  8.                            SIMPLE THINGS
  9.  
  10.  
  11. As we begin the study of C++ and object oriented programming, a few
  12. comments are in order to help you get started.  Since the field of
  13. object oriented programming is probably new to you, you will find
  14. that there is a significant amount of new terminology for you to
  15. grasp.  This is true of any new endeavor and you should be warned
  16. not to be intimidated by all of the new concepts.  We will add a
  17. few new topics in each chapter and you will slowly grasp the entire
  18. language.
  19.  
  20. Chapters one through four of this tutorial will concentrate on the
  21. non object oriented programming additions to C++.  We will not
  22. begin the discussion of any object oriented programming techniques
  23. until chapter five.
  24.  
  25.  
  26.  
  27. EVEN COMMENTS ARE IMPROVED IN C++
  28. _________________________________________________________________
  29.  
  30. Examine the file named CONCOM.CPP for an example   ==============
  31. of several new things in C++.  We will take the      CONCOM.CPP
  32. new constructs one at a time beginning with the    ==============
  33. comments.
  34.  
  35. A comment begins with the double slash "//", starts anywhere on a
  36. line, and runs to the end of that line where it is automatically
  37. terminated.  The old method of comment definition used with ANSI-
  38. C can also be used with C++ as illustrated in lines 11 through 14,
  39. among other places in this program.  The new method is the
  40. preferred method of comment definition because it is impossible to
  41. inadvertently comment out several lines of code.  This can be done
  42. by forgetting to include the end of comment notation when using the
  43. older C method of comment notation.  Good programming practice
  44. would be to use the new method for all comments and reserve the old
  45. method for use in commenting out a section of code during debugging
  46. since the two methods can be nested.
  47.  
  48. It would be well to caution you at this point however, that you
  49. should not use comments when the same sense of program definition
  50. can be obtained by using meaningful names for variables, constants,
  51. and functions.  The careful selection of variable and function
  52. names can make nearly any code self documenting and you should
  53. strive to achieve this in your code.
  54.  
  55.  
  56.  
  57.  
  58.                              Page 1-1
  59.  
  60.                     Chapter 1 - Simple Things
  61.  
  62. THE KEYWORDS const AND volatile
  63. _________________________________________________________________
  64.  
  65. There are two new keywords used in lines 9 through 11 which were
  66. not part of the original K&R definition of C, but are part of the
  67. ANSI-C standard.  The keyword const is used to define a constant.
  68. In line 9 the constant is of type int, it is named START, and is
  69. initialized to the value 3.  The compiler will not allow you to
  70. accidentally or purposefully change the value of START because it
  71. has been declared a constant.  If you had another variable named
  72. STARTS, the system would not allow you to slightly misspell STARTS
  73. as START and accidentally change it.  The compiler would give you
  74. an error message so you could fix the error.  Since it is not
  75. permissible to change the value of a constant, it is imperative
  76. that you initialize it when it is declared so it will have a useful
  77. value.  The compiler does not require you to initialize it however,
  78. and will not issue an error message if you do not.
  79.  
  80. You will note that the keyword const is also used in the function
  81. header in line 21 to indicate that the formal parameter named
  82. data_value is a constant throughout the function.  Any attempt to
  83. assign a new value to this variable will result in a compile error.
  84. This is a small thing you can add to your programs to improve the
  85. compilers ability to detect errors for you.
  86.  
  87. The keyword volatile is also part of the ANSI-C standard but was
  88. not included in the original K&R definition of C.  Even though the
  89. value of a volatile variable can be changed by you, the programmer,
  90. there may be another mechanism by which the value could be changed,
  91. such as by an interrupt timer causing the value to be incremented.
  92. The compiler needs to know that this value may be changed by some
  93. external force when it optimizes the code.  A study of code
  94. optimization methods is very interesting, but beyond the scope of
  95. this tutorial.  Note that a constant can also be volatile, which
  96. means that you cannot change it, but the system can through some
  97. hardware function.
  98.  
  99. Ignore the output statement given in line 23 for a few minutes.
  100. We will study it in some detail later in this chapter.  If you are
  101. experienced in K&R style programming, you may find line 5 and 21
  102. a little strange.  This illustrates prototyping and the modern
  103. method of function definition as defined by the ANSI-C standard.
  104. We will discuss this in great detail in chapter 4 of this tutorial.
  105. Prototyping is optional in C but absolutely required in C++.  For
  106. that reason, chapter 4 of this tutorial is devoted entirely to
  107. prototyping.
  108.  
  109. It would be advantageous for you to compile and execute this
  110. program with your C++ compiler to see if you get the same result
  111. as given in the comments at the end of the listing.  One of the
  112. primary purposes of compiling it is to prove that your compiler is
  113. loaded and executing properly.
  114.  
  115.  
  116.  
  117.                              Page 1-2
  118.  
  119.                     Chapter 1 - Simple Things
  120.  
  121. THE SCOPE OPERATOR
  122. _________________________________________________________________
  123.  
  124. The example program named SCOPEOP.CPP             ===============
  125. illustrates another construct that is new to        SCOPEOP.CPP
  126. C++.  There is no corresponding construct in      ===============
  127. either K&R or ANSI-C.  This allows access to the
  128. global variable named index even though there is
  129. a local variable of the same name within the main function.  The
  130. use of the double colon in front of the variable name, in lines 11,
  131. 13, and 16, instructs the system that we are interested in using
  132. the global variable named index, defined in line 4, rather than the
  133. local variable defined in line 8.
  134.  
  135. The use of this technique allows access to the global variable for
  136. any use.  It could be used in calculations, as a function
  137. parameter, or for any other purpose.  It is not really good
  138. programming practice to abuse this construct, because it could make
  139. the code difficult to read.  It would be best to use a different
  140. variable name instead of reusing this name, but the construct is
  141. available to you if you find that you need it sometime.
  142.  
  143. The scope operator allows access to global variables even though
  144. hidden by a local variable.  Be sure to compile and execute this
  145. program before proceeding on to the next example program where we
  146. will discuss the cout operator in lines 10, 11, 15, and 16.
  147.  
  148.  
  149. THE iostream LIBRARY
  150. _________________________________________________________________
  151.  
  152. Examine the example program named MESSAGE.CPP     ===============
  153. for our first hint of object oriented               MESSAGE.CPP
  154. programming, even though it is a very simple      ===============
  155. one.  In this program, we define a few variables
  156. and assign values to them for use in the output
  157. statements illustrated in lines 17 through 20, and in lines 23
  158. through 26.  The new operator cout is the output function to the
  159. standard device, the monitor, but works a little differently from
  160. our old familiar printf() function, because we do not have to tell
  161. the system what type we are outputting.
  162.  
  163. C++, like the C language itself, has no input or output operations
  164. as part of the language itself, but defines the stream library to
  165. add input and output functions in a very elegant manner.
  166.  
  167. The operator <<, sometimes called the "put to" operator but more
  168. properly called the insertion operator, tells the system to output
  169. the variable or constant following it, but lets the system decide
  170. how to output the data.  In line 17, we first tell the system to
  171. output the string, which it does by copying characters to the
  172. monitor, then we tell it to output the value of index.  Notice
  173. however, that we fail to tell it what the type is or how to output
  174. the value.  Since we don't tell the system what the type is, it is
  175.  
  176.                              Page 1-3
  177.  
  178.                     Chapter 1 - Simple Things
  179.  
  180. up to the system to